<?php

/**
 * SimplifyPHP Framework
 *
 * This file is part of SimplifyPHP Framework.
 *
 * SimplifyPHP Framework is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * SimplifyPHP Framework is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author Rodrigo Rutkoski Rodrigues, <rutkoski@gmail.com>
 */

/**
 *
 *
 * @author Rodrigo Rutkoski Rodrigues, <rutkoski@gmail.com>
 * @package Simplify_Kernel
 */
final class Simplify
{

  /**
   * 
   * @var IApplicationController
   */
  protected static $instance;

  /**
   * Constructor.
   * 
   * @param Config $config
   * @return void
   */
  private function __construct()
  {
  }

  /**
   *
   */
  public static function getL10n()
  {
    return self::getInstance()->getL10n();
  }

  /**
   *
   */
  public static function getConfig()
  {
    return self::getInstance()->getConfig();
  }

  /**
   *
   */
  public static function getRouter()
  {
    return self::getInstance()->getRouter();
  }

  /**
   *
   */
  public static function getRequest()
  {
    return self::getInstance()->getRequest();
  }

  /**
   *
   */
  public static function getResponse()
  {
    return self::getInstance()->getResponse();
  }

  /**
   *
   */
  public static function createInstance($app_dir, $env = 'all')
  {
    $config = new Config();
    
    $config->set('sy_dir', SY_DIR);
    $config->set('app_dir', sy_fix_path($app_dir));
    $config->set('env', $env);
    
    self::loadConfig($config, 'core');
    self::loadConfig($config, 'l10n');
    self::loadConfig($config, 'database');
    self::loadConfig($config, 'settings');
    
    /**
     * Absolute path to the web root
     */
    $config->set('www_dir', sy_fix_path(dirname($_SERVER['SCRIPT_FILENAME'])));
    
    /**
     * Absolute path to public files
     */
    $config->set('files_dir', '{www_dir}/files');
    
    /**
     * Base URL and protocol
     */
    $secure = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
    $config->set('url_protocol', substr(strtolower($_SERVER["SERVER_PROTOCOL"]), 0, strpos(strtolower($_SERVER["SERVER_PROTOCOL"]), "/")) . $secure);
    $config->set('base_url', $config->get('url_protocol') . '://' . $_SERVER['HTTP_HOST']);
    
    if (isset($_SERVER['ORIG_SCRIPT_NAME'])) {
      $config->set('app_url', preg_replace('#/$#', '', $config->get('base_url') . dirname($_SERVER['ORIG_SCRIPT_NAME'])));
    } else {
      $config->set('app_url', preg_replace('#/$#', '', $config->get('base_url') . dirname($_SERVER['SCRIPT_NAME'])));
    }
    
    //// instantiate core classes
    
    $class = $config->get('{env}:core:app:class', 'ApplicationControllerImpl');
    $app = self::$instance = new $class($config);
    
    $class = $config->get('{env}:core:router:class', 'AutoRouterImpl');
    $app->setRouter(new $class());
    
    $class = $config->get('{env}:core:request:class', 'RequestImpl');
    $app->setRequest(new $class());
    
    //$class = $config->get('{env}:core:response:class', 'ResponseImpl');
    //$app->setResponse(new $class());
    
    $class = $config->get('{env}:core:l10n:class', 'LanguageImpl');
    $app->setL10n(new $class());
    
    //Session::getInstance()->start(self::getRequest()->get('sid'));
    
    $app->initialize();
    $app->getRouter()->initialize();
    $app->getRequest()->initialize();
    //$app->getResponse()->initialize();
    $app->getL10n()->initialize();
    
    return self::$instance;
  }

  /**
   * 
   * @param $name
   * @param $category
   * @return unknown_type
   */
  public static function loadConfig(Config $config, $name)
  {
    try {
      $config->copyAllFromYAMLFile("{sy_dir}/config/$name-yaml.php");
    }
    catch (ConfigurationException $e) {
    }
    
    try {
      $config->copyAllFromYAMLFile("{app_dir}/config/$name-yaml.php");
    }
    catch (ConfigurationException $e) {
    }
  }

  /**
   *
   */
  public static function getInstance()
  {
    return self::$instance;
  }

}

?>